<?php
/**
 *
 * @author Kevin_ren <330202207@qq.com>
 * @date 2016/9/11
 * @version 1.0
 */


namespace Common\Controller;

use Think\Controller;

use Think\Page;

class BaseHomeController extends Controller
{

    /**
     * 初始化方法
     */
    public function _initialize()
    {
        $categorys = D("Category")->getAllData();
        $gossip = D("Gossip")->getAllData('', 'content');
        $num = array_rand($gossip, 1);

        $this->assign('gossip', $gossip[$num]);
        $this->assign('categorys', $categorys);
    }

    public function lists($model, $where = array(), $order = '', $base = array('is_del' => array('eq', 0)))
    {

        $options = array();
        if (is_string($model)) {
            $model = M($model);
        }

        if (empty($order)) {
            $order = 'create_time desc';
        }

        $options['where'] = array_filter(array_merge($base, $where), function ($val) {
            if ($val === '' || $val === null) {
                return false;
            } else {
                return true;
            }
        });

        $total = $model->where($options['where'])->count();


//        $listRows = C('HOME_LIST_ROWS') > 0 ? C('HOME_LIST_ROWS') : 15;
        $listRows = 13;

        $Page = new \Think\Page($total, $listRows);

        if ($total > $listRows) {
            $Page->setConfig('prev', '<span class="prev">&larr; Newer Posts</span>');
            $Page->setConfig('next', '<span class="next">Older Posts &rarr;</span>');
            $Page->setConfig('theme', '%UP_PAGE% %DOWN_PAGE%');
        }

        $show = $Page->show();
        $this->assign('page', $show);
        $limit = $Page->firstRow . ',' . $Page->listRows;

        return $model->where($options['where'])->order($order)->limit($limit)->select();
    }
} 